changequote`'changequote(`<!',`!>')dnl     Set quotes to <! and !>
#!/bin/sh

ROOT_DIR="/mnt/sysimage"
REPO_DIR="/run/install/repo"

ROOT_DEV=`df | grep ${ROOT_DIR}$ | awk -F " " '{print $1}'`
BACKUP_DEV=`df | grep ${ROOT_DIR}/srv$ | awk -F " " '{print $1}'`
DEVICE=`echo $ROOT_DEV | cut -b1-8`
BACKUP_DIR="/mnt/backup"
OS_BAK="system-backup.tar"
ALL_DISK_INSTALL=0

cat > $ROOT_DIR/etc/.productinfo <<EOF
-NeoKylin Server
-product_version_id
-loongson_64
-product_type
-B<!!>product_build_id/product_build_date
EOF

rm -rf $ROOT_DIR/etc/localtime; ln -s /usr/share/zoneinfo/Asia/Beijing $ROOT_DIR/etc/localtime
cat > $ROOT_DIR/etc/adjtime <<EOF 
0.0 0 0.0
0
LOCAL
EOF

[ -d $ROOT_DIR/DATA ] && chmod -R 777 $ROOT_DIR/DATA

tar -xf $REPO_DIR/addon/igb-modules.tar.bz2 -C $ROOT_DIR/ -m
chroot $ROOT_DIR /bin/bash -c "depmod -a 3.10.0; mkinitrd /boot/initramfs-3.10.0.img 3.10.0 -f"

cp -f $REPO_DIR/addon/qt4-qtconfig.desktop $ROOT_DIR/usr/share/applications/
echo "NoDisplay=true" >> $ROOT_DIR/usr/share/applications/remote-viewer.desktop

if [ "$ROOT_DEV" = "${DEVICE}1" ];then
    if [ -n "$BACKUP_DEV" -a "$BACKUP_DEV" = "${DEVICE}3" ];then
        ALL_DISK_INSTALL=1
    fi
else
    cd /tmp
    mkdir .root
    mount ${DEVICE}1 .root
    dev_node=`echo $ROOT_DEV | cut -b6-`
    cp -f $ROOT_DIR/boot/initramfs-3.10.0.img .root/boot/initramfs-3.10.0.img-${dev_node}
    sync
    umount .root; rmdir .root
fi

if [ $ALL_DISK_INSTALL -eq 1 ];then
    echo "start backup scripts"
    umount -l $ROOT_DIR/sys
    umount -l $ROOT_DIR/dev/pts
    umount -l $ROOT_DIR/dev/shm
    umount -l $ROOT_DIR/dev
    umount -l $ROOT_DIR/run
    umount -l $ROOT_DIR/proc
    umount -l $ROOT_DIR/DATA
    umount -l $ROOT_DIR/srv 

    cd $ROOT_DIR
    mkdir $BACKUP_DIR
    mount "${DEVICE}3" $BACKUP_DIR
    tar -cf ${BACKUP_DIR}/$OS_BAK * --selinux --acls --xattrs --atime-preserve
    echo "backup end"
    touch ${BACKUP_DIR}/backup_success
    cd /
    umount $BACKUP_DIR
fi
changequote`'dnl
